<?php
if( ! defined('BASEPATH')) exit('No direct script access allowed');

class Goals extends CI_Controller
{


	function __construct()
	{
		parent::__construct();

		// check if user logged in
		if(!$this->ion_auth->logged_in()){
			redirect('auth/login');
		}
		$this->load->library('form_validation');
		$this->load->model('founder/goals_model');
	}

	function index()
	{
		$meta['page_title'] = 'Goal';
		$this->data['objectives'] = $this->goals_model->get_objectives();
		$this->load->view('common/header', $meta);
		$this->load->view('founder/goals',$this->data);
		$this->load->view('common/footer');
	}

	/**
	* Goals creation functions
	* Created by Pratik
	*/

	function create_goal()
	{
		$meta['page_title'] = 'Create Goal';

		$this->form_validation->set_rules('user', 'User cannot be empty', 'required');
		$this->form_validation->set_rules('title', 'Title cannot be empty', 'required');
		$this->form_validation->set_rules('desc', 'Description cannot be empty', 'required');
		$this->form_validation->set_rules('objective', 'Objective cannot be empty', 'required');
		$this->form_validation->set_rules('start_date', 'Starting Date cannot be empty', 'required');
		$this->form_validation->set_rules('due_date', 'Due Date cannot be empty', 'required');

		if($this->form_validation->run() == TRUE){

			$insert = array(
				'objective_id'=> $this->input->post('objective'),
				'user_id'     => $this->input->post('user'),
				'title'       => $this->input->post('title'),
				'desc'        => nl2br($this->input->post('desc')),
				'start_date'  => $this->input->post('start_date'),
				'due_date'    => $this->input->post('due_date'),
			);
			// print_r($insert);
			$this->goals_model->create_goal($insert);
			redirect('founder/goals');
		}

		$this->data['users'] = $this->ion_auth->users()->result();
		$this->data['objectives'] = $this->goals_model->get_objectives();
		$this->load->view('common/header', $meta);
		$this->load->view('founder/create-goals',$this->data);
		$this->load->view('common/footer');
	}

	/**
	* objective funtion start here
	* @author pratik
	*/

	function create_objective()
	{
		$meta['page_title'] = 'Create Ojective';

		$this->form_validation->set_rules('title', 'Title Cannot be empty', 'required');
		$this->form_validation->set_rules('desc', 'Description Cannot be empty', 'required');
		$this->form_validation->set_rules('start_date', 'Starting Date Cannot be empty', 'required');
		$this->form_validation->set_rules('due_date', 'Due Date Cannot be empty', 'required');

		if($this->form_validation->run() == TRUE){

			$insert = array(
				'title'     => $this->input->post('title'),
				'desc'      => nl2br($this->input->post('desc')),
				'start_date'=> $this->input->post('start_date'),
				'due_date'  => $this->input->post('due_date'),
			);
			// print_r($insert);
			$this->goals_model->create_objective($insert);
			redirect('founder/goals');
		}

		$this->load->view('common/header', $meta);
		$this->load->view('founder/create-objective');
		$this->load->view('common/footer');
	}

	function edit_objective($id)
	{
		$this->form_validation->set_rules('title', 'Title Cannot be empty', 'required');
		$this->form_validation->set_rules('desc', 'Description Cannot be empty', 'required');
		$this->form_validation->set_rules('start_date', 'Starting Date Cannot be empty', 'required');
		$this->form_validation->set_rules('due_date', 'Due Date Cannot be empty', 'required');

		if($this->form_validation->run() == TRUE){
			$insert = array(
				'title'     => $this->input->post('title'),
				'desc'      => nl2br($this->input->post('desc')),
				'start_date'=> $this->input->post('start_date'),
				'due_date'  => $this->input->post('due_date'),
			);
			// print_r($insert);
			$this->goals_model->update_objective($insert,$id);
			redirect('founder/goals');
		}
		$meta['page_title'] = 'Edit';
		$data['objective'] = $this->goals_model->get_objective_by_id($id);
		$this->load->view('common/header', $meta);
		$this->load->view('founder/edit-objective',$data);
		$this->load->view('common/footer');
	}

	function goal_details($objective_id)
	{
		$data['goals'] = $this->goals_model->get_goals_by_id($objective_id);		
		$data['objec'] = $this->goals_model->get_objective_by_id($objective_id);
		$meta['page_title'] = "";
		$this->load->view('common/header', $meta);
		$this->load->view('founder/goal-details',$data);
		$this->load->view('common/footer');
	}


	function edit_goal($id)
	{

		$this->form_validation->set_rules('user', 'User cannot be empty', 'required');
		$this->form_validation->set_rules('title', 'Title cannot be empty', 'required');
		$this->form_validation->set_rules('desc', 'Description cannot be empty', 'required');
		$this->form_validation->set_rules('objective', 'Objective cannot be empty', 'required');
		$this->form_validation->set_rules('start_date', 'Starting Date cannot be empty', 'required');
		$this->form_validation->set_rules('due_date', 'Due Date cannot be empty', 'required');

		if($this->form_validation->run() == TRUE){

			$insert = array(
				'objective_id'=> $this->input->post('objective'),
				'user_id'     => $this->input->post('user'),
				'title'       => $this->input->post('title'),
				'desc'        => nl2br($this->input->post('desc')),
				'start_date'  => $this->input->post('start_date'),
				'due_date'    => $this->input->post('due_date'),
			);
			// print_r($insert);

			$this->goals_model->edit_goal($insert,$id);
			$this->session->set_flashdata('result_message', $msg.'<i class="icon-ok-sign"></i>  Result has been successfully Updated');
			redirect('founder/goals/results/'.$id);
		}

		$meta['page_title'] = "";
		$data['users'] = $this->ion_auth->users()->result();
		$data['goal'] = $this->goals_model->get_goal_details($id);
		$data['objectives'] = $this->goals_model->get_objectives();
		$this->load->view('common/header', $meta);
		$this->load->view('founder/edit-goal',$data);
		$this->load->view('common/footer');

	}
	function goal_soarting()
	{
		$action             = mysql_real_escape_string($_POST['action']);
		$updateRecordsArray = $_POST['recordsArray'];
		$this->task_model->goal_soarting($action,$updateRecordsArray);
	}

	function delete_goal($goal_id,$objective_id)
	{
		$this->goals_model->delete_goal($goal_id);
		redirect('founder/goal/goals_details/'.$objective_id,'refresh');
	}

	/* function of key results starts here */

	function results($id)
	{
		$meta['page_title'] = "";
		$data['goal'] = $this->goals_model->get_goal_details($id);
		$data['results'] = $this->goals_model->get_results($data['goal']['id']);
		$data['notes'] = $this->goals_model->get_note_by_id($data['goal']['id']);
		$this->load->view('common/header', $meta);
		$this->load->view('founder/results',$data);
		$this->load->view('common/footer');
	}

	function create_result($goal_id)
	{
		$insert = array(
			'goal_id'    => $goal_id,
			'desc'       =>  $this->input->post('result'),
			'priority'   =>$this->input->post('priority'),
			'is_achieved'=> 0,
			'is_heading' => 0,
		);
		//  print_r($insert);exit;
		$this->goals_model->create_result($insert);
		$this->session->set_flashdata('result_message', '<i class="icon-ok-sign"></i>  Result has been successfully Created');
		redirect('founder/goals/results/'.$goal_id,'refresh');
	}
	function edit_result($id)
	{
		$data['results'] = $this->goals_model->edit_result($id);
		$this->load->view('founder/edit-result',$data);
	}

	function update_result()
	{
		$update = array(
			'desc'    => $this->input->post('desc'),
			'priority'=>$this->input->post('priority')
		);
		$id        = $this->input->post('id');
		$result_id = $this->input->post('goal_id');
		$this->session->set_flashdata('result_message', '<i class="icon-ok-sign"></i>  Result has been successfully Update');
		$this->goals_model->update_result($update,$id);
		redirect('founder/goals/results/'.$result_id,'refresh');
	}

	function check_result_is_achieved()
	{
		$result_id = $_REQUEST['result_id'];
		$value     = $this->goals_model->check_result_is_achieved($result_id);
		echo $value['is_achieved'];
	}

	function result_achieved()
	{
		$result_id = $_REQUEST['result_id'];
		$value     = $this->goals_model->result_achieved($result_id);
	}

	function result_unachieved()
	{
		$result_id = $_REQUEST['result_id'];
		$value     = $this->goals_model->result_unachieved($result_id);
	}


	function completed_percentage()
	{
		// $_REQUEST['comp'];
		//echo $_REQUEST['id'];
		$this->goals_model->completed_percentage($_REQUEST['comp'],$_REQUEST['id']);
	}

	function delete_result($goal_id,$id)
	{
		$this->goals_model->delete_result($id);
		$this->session->set_flashdata('result_message', '<i class="icon-ok-sign"></i>  Result has been successfully deleted');
		redirect('founder/goals/results/'.$goal_id,'refresh');
	}

	function result_soarting()
	{
		$action             = mysql_real_escape_string($_POST['action']);
		//exit;
		$updateRecordsArray = $_POST['recordsArray'];
		$this->session->set_flashdata('result_message','<i class="icon-ok-sign"></i>  successfully Updated');
		$this->goals_model->result_soarting($action,$updateRecordsArray);

	}

	/* notes function starts here */
	function add_note()
	{
		//echo $_REQUEST['goal_id'];exit;
		$insert = array(
			'user_id'=> $this->session->userdata('user_id'),
			'goal_id'=>	$_REQUEST['goal_id'],
			'note'   =>	nl2br($_REQUEST['note']),
			'date'   => date('Y-m-d')
		);
		//print_r($insert);exit;
		$this->goals_model->add_note($insert);
		$this->session->set_flashdata('note_message', 'Note succesfully added');
	}

	function delete_note($note_id,$goal_id)
	{

		$this->goals_model->delete_note($note_id);
		$this->session->set_flashdata('note_message', 'Note deleted successfully');
		redirect('user/goals/result/'.$goal_id,'refresh');
	}


	/**
	*
	* @param undefined $goal_id
	* subheading function starts here
	*/
	function create_result_subheading($goal_id)
	{
		// $this->input->post('priority');
		$insert = array(
			'goal_id'    => $goal_id,
			'desc'       =>  $this->input->post('subheading'),
			'priority'   =>0,
			'is_achieved'=> 0,
			'is_heading' => 1,
		);
		//print_r($insert);
		$this->goals_model->create_result_subheading($insert);
		$this->session->set_flashdata('result_message', '<i class="icon-ok-sign"></i> Result Subheading has been created successfully');
		redirect('founder/goals/results/'.$goal_id,'refresh');
	}

	function delete_subheading($id,$goal_id)
	{
		$this->goals_model->delete_subheading($id);
		$this->session->set_flashdata('result_message', '<i class="icon-ok-sign"></i> Result Subheading has been deleted successfully');
		redirect('founder/goals/results/'.$goal_id,'refresh');
	}
}

?>